﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HokjesSluiten
{
    public class Map
    {
        protected int xSize = 9;
        protected int ySize = 9;

        public StreepCollection streepCollection { get; set; }

        public Map()
        {
            streepCollection = new StreepCollection(xSize, ySize);
        }

        public Map(int x, int y)
        {
            xSize = x;
            ySize = y;
            streepCollection = new StreepCollection(xSize, ySize);
        }

        public int hokjesGewonnen(Bot bot)
        {
            return streepCollection.getHokjesBy(bot).Count;
        }

        /**
         * Returns null if unsuccesfull | empty list if no hokjes | list with strepen if hokjes
         */
        public List<Streep> addStreep(Point a, Point b)
        {
            Streep streep = new Streep();
            streep.a = a;
            streep.b = b;

            return addStreep(streep);
        }

        public List<Streep> addStreep(Point a, Point b, Bot bot)
        {
            Streep streep = new Streep();
            streep.a = a;
            streep.b = b;
            streep.moveMadeBy = bot;

            return addStreep(streep);
        }

        public List<Streep> addStreep(Streep streep, Bot bot)
        {
            streep.moveMadeBy = bot;

            return addStreep(streep);
        }

        public List<Streep> addStreep(Streep streep)
        {
            if (streep.a.X < 0 || streep.a.Y < 0 || streep.b.X < 0 || streep.b.Y < 0
                || streep.a.X > xSize || streep.a.Y > ySize || streep.b.X > xSize || streep.b.Y > ySize)
                return null;

            if (!streepCollection.addStreep(streep))
                return null;

            return checkHokjes(streep);
        }

        public int movesLeft()
        {
            return (((xSize + 1) * ySize) + ((ySize + 1) * xSize)) - streepCollection.countStrepen();
        }

        public int hokjesLeft()
        {
            return ((xSize - 1) * (ySize - 1)) - streepCollection.countHokjes();
        }

        protected List<Streep> checkHokjes(Streep streep)
        {
            List<Streep> hokjes = new List<Streep>();
            if (streep.a.X == streep.b.X)
            {
                if (streep.a.X < xSize)
                {
                    Streep hok = streepCollection.hokjeRight(streep);                   
                    if (hok != null)
                        hokjes.Add(hok);
                }

                if (streep.a.X > 0)
                {
                    Streep hok = streepCollection.hokjeLeft(streep);
                    if (hok != null)
                        hokjes.Add(hok);
                }
            }
            else if (streep.a.Y == streep.b.Y)
            {
                if (streep.a.Y < ySize)
                {
                    Streep hok = streepCollection.hokjeBelow(streep);
                    if (hok != null)
                        hokjes.Add(hok);
                }

                if (streep.a.Y > 0)
                {
                    Streep hok = streepCollection.hokjeAbove(streep);
                    if (hok != null)
                        hokjes.Add(hok);
                }
            }

            return hokjes;
        }
    }
}
